/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ipfms.workflow;

import com.google.inject.Inject;
import ipfms.dao.BuildingDAO;
import ipfms.dao.PenggunaDAO;
import ipfms.manager.InboxManager;
import ipfms.model.Building;
import ipfms.model.Inbox;
import ipfms.model.InfoAudit;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author fikri
 */
public class Workflow {
      private static final Map<Integer, Step> steps = new HashMap<Integer, Step>();

      static {
          steps.put(0, new Step(0, 1, WorkflowStatus.OWNER, "New Property", null, false));
          steps.put(1, new Step(1, 2, WorkflowStatus.SALES, "New Property", null, false));
          steps.put(2, new Step(2, 3, WorkflowStatus.PMO, "New Property", null, false)); 
          steps.put(3, new Step(2, 9999, WorkflowStatus.AGENT, "New Property", null, false));          
      }

      @Inject InboxManager inboxManager;      
      @Inject PenggunaDAO penggunaDAO;
      @Inject BuildingDAO buildingDAO;
      
      
      public void doAction(int step, Map<String,String> input) {
          
          String fromUserId = input.get("fromUserId");
          String idBuilding = input.get("idBuilding");
          String mesej = input.get("mesej");
          String publish = input.get("publish");


          Step currentStep = getStep(step);
          Step nextStep = getStep(currentStep.getNextId());

          Building building = buildingDAO.findById( Integer.valueOf(idBuilding) );
          InfoAudit ia = new InfoAudit();

          if (nextStep != null) { //9999 = finish TODO

              Inbox inbox = inboxManager.getWorkflowInfo(nextStep.getStatus().name(), idBuilding);
              if (inbox == null) {
                  ia.setTarikhMasuk(new Date());
                  ia.setDimasukOleh(penggunaDAO.findById(fromUserId));
                  inbox = new Inbox();
                  inbox.setActive('Y');
                  inbox.setWorkflowId(nextStep.getStatus().name());
                  inbox.setBuilding(building);                  
//                  fasa.setUlasan("Permohonan Saudara Baru <br/>" + idPermohonan);
                  inbox.setInfoAudit(ia);
                  inbox.setSendDate(new Date());                  
              } else {
                  ia = inbox.getInfoAudit();
                  ia.setDiKemaskiniOleh((penggunaDAO.findById(fromUserId)));
                  ia.setTarikhKemaskini(new Date());
                  inbox.setActive('Y');
                  inbox.setWorkflowId(nextStep.getStatus().name());
                  inbox.setSendDate(new Date());
                  if (StringUtils.isNotBlank(mesej))
                      inbox.setMessage(mesej);
              }              
              inboxManager.save(inbox);
          } 
          
          if (StringUtils.isBlank(publish)) {
              Inbox in = inboxManager.getWorkflowInfo(currentStep.getStatus().name(), idBuilding);
              if (in != null) {
                  in.setActive('T');
                  inboxManager.save(in);
              }
          }          
      }

      private Step getStep(int step) {
        return steps.get(step);
    }

}
